<template>
  <div class="home">
    <div class="detail_box">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}" @click="chooseTab(index,item.id)">{{item.name}}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>仓库：</label>
            <el-select
              filterable
              v-model="warehouseId"
              placeholder="请选择仓库">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>订单号：</label>
            <el-input type="text" placeholder="请输入订单号" v-model="inventoryOrderNo" clearable></el-input>
          </div>
          <div class="operate_item">
            <div class="operate_item" v-if="tabMindex == -1">
              <label>状态：</label>
              <el-select
                filterable
                clearable
                @change="getmIndex"
                v-model="enableType"
                placeholder="请选择状态">
                <el-option
                  v-for="(item,index) in statusOption"
                  :key="index"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>

            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="searchData">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" >
          <span class="operate_btn search_btn" @click="onNew('')" v-if="btn&&btn.add">生成订单</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part">
          <div class="table_box">
            <el-table
              border
              height="620"
              v-loading="loading"
              :header-cell-style="tableHeader"
              ref="multipleTable"
              :data="tableInfo"
              tooltip-effect="dark"
              highlight-current-row
              style="width: 100%;">

              <el-table-column
                width="160"
                sortable="custom"
                prop="createTime"
                label="创建时间">
                <template slot-scope="scope">
                  <p style="text-align: left">{{formatTime(scope.row.createTime)}}</p>
                </template>
              </el-table-column>

              <el-table-column
                prop="warehouseName"
                label="仓库名">
                <template slot-scope="scope">{{scope.row.warehouseName?scope.row.warehouseName:'——'  + scope.row.remark+ scope.row.remark ? `(${scope.row.remark})`:''}}</template>
              </el-table-column>

              <el-table-column
                width="230"
                prop="inventoryOrderNo"
                label="订单号">
              </el-table-column>
              <el-table-column
                prop="operateUserName"
                label="操作人">
                <template slot-scope="scope">{{scope.row.operateUserName?scope.row.operateUserName:'——'}}</template>
              </el-table-column>
              <el-table-column
                prop="remark"
                label="备注">
                <template slot-scope="scope">{{scope.row.remark?scope.row.remark:'——'}}</template>
              </el-table-column>
              <el-table-column
                width="80"
                label="状态">
                <template slot-scope="scope">
                  <div :style="'color:'+(scope.row.enable===2?'orange':(scope.row.enable===1?'#1FE305':(scope.row.enable === -1? '#409EFF':'#FB2020' )))">
                    {{scope.row.enable===2?'待确认':(scope.row.enable===1?'已确认':(scope.row.enable === -1 ? '已完成' : '已过期'))}}
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="操作"
                fixed="right"
                width="220" v-if="btn.detail || btn.update || btn.delete || btn.confirm">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span class="change_info" @click="openPopup(scope.row,1)" v-if="btn.detail">详情</span>
                    <span class="change_info" @click="onNew(scope.row)" v-if="btn.update && (scope.row.enable== 2)">编辑</span>
                    <span class="change_info success_btn" @click="openPopup(scope.row,2)" v-if="btn.confirm && (scope.row.enable== 2) ">确认</span>
<!--                    <span class="change_info delete_btn" @click="deleteOrder(scope.row)" v-if="btn.delete && (scope.row.enable == 2 || scope.row.enable == 3)">删除</span>-->
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{totals}}</i>条</span>
            <el-pagination
              @size-change="sizeChange"
              @current-change="getTableInfo"
              background
              :current-page="currentPage"
              :page-sizes="[15, 20, 30, 40]"
              :page-size="pageSize"
              layout="prev, pager, next, sizes,jumper"
              :total="totals">
            </el-pagination>
          </div>
        </div>
      </div>

    </div>
    <createOrder :id="mid" v-if="isCreate" @listenToChild="listenToEdit" :remarks="remark"></createOrder>
    <detail v-if="isPop" :id="mid" :isConfirm="isConfirm" :confirmId="confirmId" :remark="remark" @listenToChild="listenToEdit"></detail>
  </div>
</template>

<script>

import detail from './detail'
import createOrder from "./createOrder";
import commons from "@/utils/common";
import {listData,delOrder} from "@/api/basic/warehouse/inventory";
import {getBtn} from "@/utils/btnAuth";
import {judgeRole} from "@/utils/warehouseAuth";

export default {
  components: {
    detail,
    createOrder,
  },
  data() {
    return {
      statusOption:[{name:'已确认',id:1},{name:'已过期',id:3}],
      menus:[{name:'待确认',id:2},{name:'已完成',id:-1}],
      warehouseId:"",
      mIndex:2,
      proDate:'',
      inventoryOrderNo:'',
      userId:'',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo:[],
      isPop: false,
      orderId: '',
      orderNo:'',
      uId:'',
      oId:'',
      isCreate:false,
      isSend:false,
      btn:{},
      update:false,
      isRemark:false,
      remark:'',
      types:1,
      type:'',
      tabIndex:0,
      keyword:'',
      mid:'',
      enableType:'',
      tabMindex:'',
      warehouseOption:[],
      isConfirm: false,
      confirmId: ''
    }
  },
  mounted() {
    //用户列表过来
    // if(this.$route.query.userId) {
    //   this.userId = parseInt(this.$route.query.userId);
    // }
    this.roleHouse = judgeRole();
    this.warehouseOption = this.roleHouse.list;
    this.warehouseId = this.roleHouse.id;
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
  },
  methods: {
    chooseTab(index,id){
      this.enableType = '';
      this.mIndex = id;
      this.tabIndex = index;
      this.tabMindex = id;
      this.getTableInfo(1);
    },
    getmIndex(enable) {
      this.mIndex = enable;
    },

    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit:that.pageSize,
        page:page,
        inventoryOrderNo:that.inventoryOrderNo,
        userId:that.userId,
        enable:that.mIndex,
        warehouseId:that.warehouseId,
        startDate:that.proDate?that.proDate[0]:'',
        endDate:that.proDate?that.proDate[1]:'',
      }
      listData(queryParams).then(res => {
        that.tableInfo = res?res.list:[];
        that.totals = res?res.count:0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    searchData(){
      this.isPop = false;
      this.getTableInfo(1)
    },
    onNew(obj) {
      this.mid = obj.inventoryOrderNo;
      this.remark = obj.remark;
      this.isCreate = true;
    },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.userId = '';
      this.proDate = '';
      this.inventoryOrderNo = '';
      this.warehouseId = this.roleHouse.id?this.roleHouse.id:'';
      if(this.mIndex == 1 || this.mIndex == 3) {
        this.mIndex = -1;
      }else {
        this.mIndex = 2;
      }
      this.searchData()
    },

    // 打开弹窗
    openPopup(obj,type) {
      this.isConfirm = type===2;
      this.isPop = true;
      this.remark = obj.remark;
      this.status = obj.enable;
      this.mid = obj.inventoryOrderNo;
      this.confirmId = type===2?obj.id:'';
    },

    sendPopup(obj){
      this.isSend = true;
      this.oId = obj.orderNo;
      this.uId = obj.userId;
    },

    // 删除
    deleteOrder(obj,type) {
      let that = this;
      that.type = type;
      that.$confirm('确定删除盘点单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delOrder(obj.id);
      }).then(()=> {
        that.getTableInfo(that.currentPage);
        that.$message.success({
          message:'删除成功！',
          duration:1500
        });
      }).catch(() => {
      });
    },

    listenToEdit(e){
      let that = this;
      that.isCreate = false;
      that.isPop = false;
      if (e){
        that.getTableInfo(that.currentPage)
      }
    },

    formatTime(time){
      return commons.formatTime(time)
    },

    sizeChange(val){
      this.pageSize =val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>
.flex_box {
  display: flex;
}
.detail_part {
  width: 100%;
}



</style>
